// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

package com.tencent.nanodetncnn;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.text.SimpleDateFormat;

public class MainActivity extends Activity implements SurfaceHolder.Callback {
    public static final int REQUEST_CAMERA = 100;
    public static final int REQUEST_READ = 101;
    private static final String rtspUrl = "rtsp://admin:123456@192.168.1.11/stream0";

    private final NanoDetNcnn nanodetncnn = new NanoDetNcnn() {
        @Override
        public void callback(String output) {
            Log.d("ncnn", "识别结果：" + output);
        }
    };

    private int current_cpugpu = 0;

    private SurfaceView cameraView;
    private boolean isOpen = false;
    private Thread cameraThread;
    private boolean isRecord;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        cameraView = (SurfaceView) findViewById(R.id.cameraview);

        cameraView.getHolder().setFormat(PixelFormat.RGBA_8888);
        cameraView.getHolder().addCallback(this);

        Button buttonSwitchCamera = (Button) findViewById(R.id.buttonSwitchCamera);
        buttonSwitchCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                nanodetncnn.disconnectCamera();
                nanodetncnn.connectCamera(rtspUrl);
            }
        });

        findViewById(R.id.button_test_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        long start = SystemClock.elapsedRealtime();
//                        final Bitmap b = BitmapFactory.decodeFile("/sdcard/bus.jpg");
                        final Bitmap b = BitmapFactory.decodeFile("/sdcard/test.jpg");
//                        final Bitmap b = BitmapFactory.decodeFile("/sdcard/2.jpg");
                        for (int i = 0; i < 5; i++) {
                            nanodetncnn.testImage(b);
                        }
                        Log.w("MainActivity", String.format("耗时:%dms", (SystemClock.elapsedRealtime() - start) / 5));

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ImageView img = (ImageView) findViewById(R.id.imageview);
                                img.setImageBitmap(b);
                            }
                        });
                    }
                }).start();
            }
        });


        Button btn = findViewById(R.id.openCamera);
        btn.setOnClickListener(view -> {
            if (!isOpen) {
                isOpen = true;
                cameraThread = new Thread(() -> nanodetncnn.connectCamera(rtspUrl));
                cameraThread.start();
                btn.setText("关闭摄像头");
            } else {
                isOpen = false;
                nanodetncnn.disconnectCamera();
                cameraThread.interrupt();
                btn.setText("开启摄像头");
            }
        });

        Button record = findViewById(R.id.record);
        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isRecord){
                    isRecord = true;
                    String s = new SimpleDateFormat("yyyyMMddHHmmss").format(System.currentTimeMillis());
                    String pathName = "/sdcard/" + "10000000" + "_" + s + ".h264";
                    nanodetncnn.startRecord(pathName);
                    record.setText("停止录像");
                }else {
                    isRecord = false;
                    nanodetncnn.stopRecord();
                    record.setText("开始录象");
                }
            }
        });

        Spinner spinnerCPUGPU = (Spinner) findViewById(R.id.spinnerCPUGPU);
        spinnerCPUGPU.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
                if (position != current_cpugpu) {
                    current_cpugpu = position;
                    reload();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        reload();
    }

    private void reload() {
        boolean ret_init = nanodetncnn.loadModel(getAssets(), current_cpugpu);
        if (!ret_init) {
            Log.e("MainActivity", "nanodetncnn loadModel failed");
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        nanodetncnn.setOutputWindow(holder.getSurface());
        Log.d("TAG", "surfaceChanged: 图像切换");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    @Override
    public void onResume() {
        super.onResume();

        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
        }

        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ);
        }
        nanodetncnn.initCallback();
    }

    @Override
    public void onPause() {
        super.onPause();
        nanodetncnn.disconnectCamera();
    }
}
